<?php
declare (strict_types = 1);

namespace app\api\controller;


use app\api\validate\User;
use think\exception\ValidateException;

class Login extends Base {


    public function index() {
        if (!$this->request->isPost()) {
            return show(config("status.error"), "请求方式错误");
        }



        $tel = $this->request->param("tel", "", "trim");
        $code = $this->request->param("code", "", "trim");
        $type = $this->request->param("type", 0, "intval");

        $data = [
            "tel" => $tel,
            "code" => $code,
            "type" => $type,
            "ip" => $this->request->ip()
        ];
        try{
            validate(User::class)->scene("login")->check($data);
        }catch (ValidateException $e){
            return show(config("status.error"), $e->getError());
        }

        //验证通过之后登陆
        $business = new \app\api\business\User();
        if (!$business->login($data)) {
            return show(config("status.error"), $business->getError());
        }

        return show(config("status.success"), "登录成功", $business->getUserInfo());
    }
}